#include "ctaconfig.h"
#include <QFile>
#include <QMessageBox>
#include <string>
#include "game/parser.h"

ctaConfig::ctaConfig()
{
    XResolution = 640;
    YResolution = 480;
    BPP = 16;
    FullScreen = false;
}

bool ctaConfig::readConfig()
{

    QFile file(QDir::homePath().append("/.cta/config.xml"));
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        file.setFileName(QDir::currentPath().append("/data/config/config.xml"));
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            XResolution = 640;
            YResolution = 480;
            BPP = 16;
            return false;
        }
    }
    QString configFileQString = file.readAll();
    std::string configFileString = configFileQString.toStdString();
    XResolution = cta::parser::getIntegerFromString( configFileString, "ScreenResolution", "Width" );
    YResolution = cta::parser::getIntegerFromString( configFileString, "ScreenResolution", "Height" );
    BPP = cta::parser::getIntegerFromString( configFileString, "ScreenResolution", "BPP" );
    FullScreen = cta::parser::getIntegerFromString( configFileString, "ScreenResolution", "FullScreen" );

    return true;
}

bool ctaConfig::saveConfig()
{
    QFile file(QDir::homePath().append("/.cta/config.xml"));
    QMessageBox::information(0,"Save Options", QString().append(
            "Write Options to File: ").append(file.fileName()));
    if(!file.open( QIODevice::WriteOnly | QIODevice::Text))
    {
        QMessageBox::warning(0, "Fehler beim Speichern",
                             "Options können nicht in Datei ($home/.cta/config.xml) geschrieben werden");
        return false;
    }
    QString xmlstring;
        // write xmlfile into stringstream
        xmlstring.append( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" ).append( "\n" );
        xmlstring.append( "<cta>" ).append( "\n" );
        xmlstring.append( "\t<ScreenResolution" ).append( "\n" );
        xmlstring.append( "\t\tWidth=\"" ).append(QString::number(XResolution)).append( "\"" ).append( "\n" );
        xmlstring.append( "\t\tHeight=\"" ).append(QString::number(YResolution)).append( "\"" ).append( "\n");
        xmlstring.append( "\t\tBPP=\"" ).append(QString::number(BPP)).append( "\"" ).append( "\n" );
        xmlstring.append( "\t\tFullScreen=\"" ).append(QString::number(FullScreen)).append( "\"" ).append( "\n" );
        xmlstring.append( "\t\t>" ).append( "\n" );
        xmlstring.append( "\t</ScreenResolution>" ).append( "\n" );
        xmlstring.append( "</cta>" );
    file.write(xmlstring.toAscii());
    return true;
}

bool ctaConfig::saveCSConfig()
{
    QFile file(QDir::tempPath().append("/cta.cfg"));
    if(!file.open( QIODevice::WriteOnly | QIODevice::Text))
    {
        QMessageBox::warning(0, "Fehler beim Speichern",
                             "Options können nicht in temporäre Datei ($TEMP/cta.cfg) geschrieben werden");
        return false;
    }
    QString string;
        // write xmlfile into stringstream
    // System Plugin Part
        string.append( ";cs plugins" ).append( "\n");

        string.append( "System.Plugins.iGraphics3d = crystalspace.graphics3d.opengl \n" );
        string.append( "System.Plugins.iConsoleInput = crystalspace.console.input.standard \n");
        string.append( "System.Plugins.iConsoleOutput = crystalspace.console.output.standard \n");
        string.append( "System.Plugins.iEngine = crystalspace.engine.3d \n");
        string.append( "System.ApplicationID = CTA \n");



    // Video Part
        string.append( "; Video Settings" ).append( "\n" );
        string.append( "Video.ScreenWidth = " ).append(QString::number(XResolution)).append( "\n" );
        string.append( "Video.ScreenHeight = ").append(QString::number(YResolution)).append( "\n" );
    if(this->FullScreen)
        string.append("Video.FullScreen = yes").append("\n");

    file.write(string.toAscii());
    return true;
}
